package com.liuche.pictureserver.process;

import com.liuche.pictureserver.pojo.ProcessModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 责任链模板
 *
 * @author liuche
 */
public class ProcessTemplate {
    private final List<ProcessHandler> processList = new ArrayList<>();

    /**
     * 添加责任链处理器
     *
     * @param handler 处理器
     */
    public void addHandler(ProcessHandler handler) {
        processList.add(handler);
    }

    /**
     * 执行责任链
     *
     * @param context 上下文
     * @return 责任链执行完信息
     */
    public <T extends ProcessModel> ProcessContext<T> process(ProcessContext<T> context) {
        for (ProcessHandler processHandler : processList) {
            processHandler.run(context);
            if (context.isBreakFlag()) {
                break;
            }
        }

        return context;
    }
}